/**
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
package com.cron.job;

import com.conf.Main;
import java.sql.Connection;
import java.util.HashMap;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * Classe Base per l'uso dei job-cron
 *
 * @author Marco Berri marcoberri@gmail.com
 */
public class Base implements Job {

    /**
     * configurazioni globali
     */
    protected Main conf = null;
    /**
     * configurazioni globali
     */
    protected HashMap<String, String> properties;
    /**
     * root della app
     */
    protected String path_app_root;
    /**
     * logger
     */
    protected org.apache.log4j.Logger log = null;
    /**
     * connessione al db
     */
    protected Connection conn_url;
    /**
     * connessione al db delle statistiche
     */
    protected Connection conn_stats;

    /**
     * esegue il conjob
     * @param context
     * @throws JobExecutionException
     */
    public void execute(JobExecutionContext context) throws JobExecutionException {

        if (context.getJobDetail().getJobDataMap().get("conf") != null) {
            this.conf = (Main) context.getJobDetail().getJobDataMap().get("conf");
        }
        if (context.getJobDetail().getJobDataMap().get(com.conf.Main.TAG_XML_CRON_JOB_PROPERTIES) != null) {
            this.properties = (HashMap<String, String>) context.getJobDetail().getJobDataMap().get(com.conf.Main.TAG_XML_CRON_JOB_PROPERTIES);
        }
        if (context.getJobDetail().getJobDataMap().get("path_app_root") != null) {
            this.path_app_root = (String) context.getJobDetail().getJobDataMap().get("path_app_root");
        }
        if (context.getJobDetail().getJobDataMap().get(com.conf.Log.TAG_XML_LOG) != null) {
            this.log = (org.apache.log4j.Logger) context.getJobDetail().getJobDataMap().get(com.conf.Log.TAG_XML_LOG);
        }
        if (context.getJobDetail().getJobDataMap().get("conn_url") != null) {
            this.conn_url = (Connection) context.getJobDetail().getJobDataMap().get("conn_url");
        }
        if (context.getJobDetail().getJobDataMap().get("conn_stats") != null) {
            this.conn_stats = (Connection) context.getJobDetail().getJobDataMap().get("conn_stats");
        }
    }

    /**
     * log di debug con exception
     * @param text
     * @param e
     */
    protected void debug(String text, Exception e) {

        text = this.getClass().getName() + " - " + (bytesToMeg(Runtime.getRuntime().totalMemory()) - bytesToMeg(Runtime.getRuntime().freeMemory())) + "/" + bytesToMeg(Runtime.getRuntime().totalMemory()) + " - " + text;

        if (e == null) {
            this.log.debug(text);
        } else {
            this.log.debug(text, e);
        }
    }

    /**
     * log di debug
     * @param text
     */
    protected void debug(String text) {
        debug(text, null);
    }

    /**
     *
     * @param text
     * @param e
     */
    protected void fatal(String text, Exception e) {

        text = this.getClass().getName() + " - " + (bytesToMeg(Runtime.getRuntime().totalMemory()) - bytesToMeg(Runtime.getRuntime().freeMemory())) + "/" + bytesToMeg(Runtime.getRuntime().totalMemory()) + " - " + text;

        if (e == null) {
            this.log.fatal(text);
        } else {
            this.log.fatal(text, e);
        }
    }

    /**
     * log di fatal
     *
     * @param text
     */
    protected void fatal(String text) {
        fatal(text, null);
    }

    /**
     * conversione da byte a Mb
     * @param bytes
     * @return long
     */
    public static long bytesToMeg(long bytes) {
        return bytes / (1024L * 1024L);
    }
}
